#include <windows.h>

#include "lib.h"
#include "apppath.h"
#include "dir.h"

PathName AppPath::exeDir;
PathName AppPath::patchDir;
PathName AppPath::tempDir;
PathName AppPath::manifestDir;
PathName AppPath::fontDir;

int32	 AppPath::refCount = 0;

/**
 * Initializes application directories
 */
void
AppPath::init()
{
	if (++refCount == 1)
	{	
		wchar_t buf[MAX_PATH];
		int32 len;

		len = GetModuleFileName((HMODULE)0, buf, MAX_PATH);
		buf[len] = '\0';
		exeDir.set(buf);
		exeDir.removeEnd();
		exeDir.concat("\\");
		
		tempDir.set(exeDir);
		tempDir.concat("temp");
		Directory::make(tempDir);

		patchDir.set(exeDir);
		patchDir.concat("_patch");
		Directory::make(patchDir);

		manifestDir.set(exeDir);
		manifestDir.concat("installed");
		Directory::make(manifestDir);

		fontDir.set(exeDir);
		fontDir.concat("fonts");
		Directory::make(fontDir);
	}
}

/**
 * Destroys application directories
 */
void
AppPath::destroy()
{
	ASSERT(refCount > 0);

	if (--refCount == 0)
	{	
		exeDir.makeEmpty();
		tempDir.makeEmpty();
		patchDir.makeEmpty();
		manifestDir.makeEmpty();
		fontDir.makeEmpty();
	}
}
